//
//  ChildMainPlayViewController.m
//  XMLY
//
//  Created by lanou on 15/11/5.
//  Copyright © 2015年 曾杰. All rights reserved.
//

#import "ChildMainPlayViewController.h"
#import "PKPlayerManger.h"
#import "ChildListModel.h"
#import "PlayView.h"
@interface ChildMainPlayViewController ()
@property (nonatomic, strong) PlayView *mainPlayView;
@property (nonatomic, strong) NSMutableArray *musicUrlArr;
@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, assign) BOOL playState;
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation ChildMainPlayViewController
- (NSMutableArray *)musicUrlArr{
    if (_musicUrlArr == nil) {
        _musicUrlArr = [NSMutableArray array];
    }
    return _musicUrlArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.mainPlayView = [[PlayView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_mainPlayView];
    
    for (ChildListModel *model in self.playListUrlArr) {
        [self.musicUrlArr addObject:model.playUrl32];
    }
    [[PKPlayerManger shareManger] setMangerMusicArray:self.musicUrlArr];
    _currentIndex = self.index;
    _playState = YES;
     [[PKPlayerManger shareManger] playWithIndex:_currentIndex];
     [self setImageViewWithindex:_currentIndex];
    [_mainPlayView.backBtn addTarget:self action:@selector(didClickBackBtn) forControlEvents:(UIControlEventTouchUpInside)];
    [_mainPlayView.beforeBtn addTarget:self action:@selector(didClickBeforeBtn) forControlEvents:(UIControlEventTouchUpInside)];
    [_mainPlayView.playAndPause addTarget:self action:@selector(didClickPlayAndPauseBtn:) forControlEvents:(UIControlEventTouchUpInside)];
    [_mainPlayView.nextBtn addTarget:self action:@selector(didClickNextBtn) forControlEvents:(UIControlEventTouchUpInside)];
    [_mainPlayView.slider addTarget:self action:@selector(sliderBeginSlide:) forControlEvents:(UIControlEventValueChanged)];
    _timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSDefaultRunLoopMode];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)didClickBackBtn{
    NSLog(@"退出");
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didClickBeforeBtn{
    NSLog(@"上一曲");
    if (_currentIndex == 0) {
        _currentIndex = self.musicUrlArr.count;
    }
    _currentIndex -= 1;

    [[PKPlayerManger shareManger] playWithIndex:_currentIndex];
     [self setImageViewWithindex:_currentIndex];
}
- (void)didClickPlayAndPauseBtn:(UIButton *)sender{
    NSLog(@"暂停/播放");
    [[PKPlayerManger shareManger] playAndPause];
    
    if (_playState && sender.selected == NO) {
        [_mainPlayView.playAndPause setImage:[UIImage imageNamed:@"zanting"] forState:(UIControlStateNormal)];
        _playState = NO;
        
    }else{
        [_mainPlayView.playAndPause setImage:[UIImage imageNamed:@"bofang"] forState:(UIControlStateNormal)];
       
        _playState = YES;
         [self setImageViewWithindex:_currentIndex];
        
    }
    sender.selected = !sender.selected;

}
- (void)didClickNextBtn{
    NSLog(@"下一曲");
    _currentIndex += 1;
    if (_currentIndex == self.musicUrlArr.count) {
        _currentIndex = 0;
    }
   
    [[PKPlayerManger shareManger] playWithIndex:_currentIndex];
    [self setImageViewWithindex:_currentIndex];
}

// slider滑动
- (void)timeChange{
    PKPlayerManger *player = [PKPlayerManger shareManger];
    _mainPlayView.slider.minimumValue = 0.0;
    _mainPlayView.slider.maximumValue = player.finishTime;
    
    NSUInteger yuTime = player.finishTime - player.currentTime;
    NSString *minutes = [NSString stringWithFormat:@"%02ld", yuTime / 60];
    NSString *seconds = [NSString stringWithFormat:@"%02ld", yuTime % 60];
    _mainPlayView.timeLabel.text = [[minutes stringByAppendingString:@":"] stringByAppendingString:seconds];
    _mainPlayView.slider.value = player.currentTime;
    
    if (player.currentTime !=0 && player.finishTime-player.currentTime==0) {
        [self didClickNextBtn];
        
    }
}
// 跳动到某一个时间点
- (void)sliderBeginSlide:(UISlider *)slider{
    PKPlayerManger *playerManger = [PKPlayerManger shareManger];
    [playerManger seekToTime:slider.value];
    
}

// 设置图片
- (void)setImageViewWithindex:(NSInteger)index{
    ChildListModel *model = self.playListUrlArr[index];
    [_mainPlayView configureViewWithModel:model];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
